let db = null;

let set_db = function (d) {
	db = d;
}

//查询数据模型权限
let get_datamodel_data_rights = async function (datamodel, user_id) {
	let sql = "select df.*,r.filter from sys_data_func df left outer join sys_role_data_func_r r on df.id=r.data_func_id where \
			df.datamodel_id=? and df.is_deleted=0 and r.is_deleted=0 and \
			r.role_id in (select role_id from sys_user_role_r where is_deleted=0 and user_id=?)";
	let result = await db.query(sql, [datamodel, user_id]);
	let rights = {};
	for (let row of result.rows) {
		if (!rights[row.name]) rights[row.name] = true;
		if (row.filter) {
			let filter = JSON.parse(row.filter);
			if (filter.hidden_columns && rights[row.name].hidden_columns) {
				rights[row.name].hidden_columns = rights[row.name].hidden_columns.concat(filter.hidden_columns);
			}
			else if (filter.hidden_columns) {
				rights[row.name].hidden_columns = filter.hidden_columns;
			}
			if (filter.row_conditions && rights[row.name].row_conditions) {
				rights[row.name].row_conditions = rights[row.name].row_conditions.concat(filter.row_conditions);
			}
			else if (filter.row_conditions) {
				rights[row.name].row_conditions = filter.row_conditions;
			}
		}
	}
	return (rights);
}

//查询数据库表权限
let get_table_data_rights = async function (tablename, user_id) {
	let sql = "select df.*,r.filter from sys_data_func df left outer join sys_role_data_func_r r on df.id=r.data_func_id where \
			df.datamodel_id in (select id from sys_datamodel where tablename=? and is_deleted=0) and df.is_deleted=0 and r.is_deleted=0 and \
			r.role_id in (select role_id from sys_user_role_r where is_deleted=0 and user_id=?)";
	let result = await db.query(sql, [tablename, user_id]);
	let rights = {};
	for (let row of result.rows) {
		if (!rights[row.name]) rights[row.name] = true;
		if (row.filter) {
			let filter = JSON.parse(row.filter);
			if (filter.hidden_columns && rights[row.name].hidden_columns) {
				rights[row.name].hidden_columns = rights[row.name].hidden_columns.concat(filter.hidden_columns);
			}
			else if (filter.hidden_columns) {
				rights[row.name].hidden_columns = filter.hidden_columns;
			}
			if (filter.row_conditions && rights[row.name].row_conditions) {
				rights[row.name].row_conditions = rights[row.name].row_conditions.concat(filter.row_conditions);
			}
			else if (filter.row_conditions) {
				rights[row.name].row_conditions = filter.row_conditions;
			}
		}
	}
	return (rights);
}

//是否有所有数据库表相关数据权限
let has_right_of_all_tables = async function (right, tables, user_id) {
	let sql = "select distinct(df.datamodel_id) as datamodel_id from sys_data_func df left outer join sys_role_data_func_r r on df.id=r.data_func_id where \
			df.name=? and df.datamodel_id in (select id from sys_datamodel where tablename in (?) and is_deleted=0) and \
			df.is_deleted=0 and r.is_deleted=0 and \
			r.role_id in (select role_id from sys_user_role_r where is_deleted=0 and user_id=?)";
	let result = db.query(sql, [right, tables, user_id]);
	return (result.rows && result.rows.length == tables.length);
}


module.exports = { get_datamodel_data_rights, get_table_data_rights, has_right_of_all_tables, set_db };